<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Order extends Eloquent
{
	use SoftDeletingTrait;

	public $timestamps   = true;
	protected $dates     = ['deleted_at'];
	protected $table     = 'order';
	protected $validator = null;

	public function saveData($data)
	{
		$order = new Order;

		if(isset($data['id'])){
			$order = Order::firstOrCreate(array('id' => $data['id']));
		}else{
			$order->order_number = $this->getOrderNumber($data['type']);
		}

		$date = DateTime::createFromFormat('d-m-Y', $data['date']);
        $date = $date->format("Y-m-d");

		$order->customer = $data['customer'];
		$order->subtotal = $data['subtotal'];
		$order->total    = $data['total'];
		$order->discount = $data['discount'];
		$order->date     = $date;
		$order->type     = $data['type'];

		$order->save();

		return $order;
	}

	protected function getOrderNumber($type){
		$max = DB::table('order')
				->whereType($type)
				->max('order_number') + 1;

		return $max;
	}

	public function getDept($customer, $date, $order_id = null){
		$date = DateTime::createFromFormat('d-m-Y', $date);
        $date = $date->format("Y-m-d");
        
        if($order_id != null){
        	$order_ids_1 = DB::table('order')->where('id', '<>', $order_id)->where('date', '<', $date)->where('customer', '=', $customer)->whereNull('deleted_at')->lists('id');

        	$order_ids_2 = DB::table('order')->where('id', '<>', $order_id)->where('date', '=', $date)->where('customer', '=', $customer)->whereNull('deleted_at')->lists('id');

        	$order_ids = array_merge($order_ids_1, $order_ids_2);

        	if(count($order_ids) == 0){
				$dept = 0;
			}else{
				$dept = DB::table('order')->whereIn('id', $order_ids)->whereNull('deleted_at')->sum('total');
				$payment = DB::table('customer_payment')->whereIn('order', $order_ids)->whereNull('deleted_at')->sum('total');

				$dept -= $payment;
			}
        }else{
        	$dept = DB::table('order')->where('date', '<=', $date)->where('customer', '=', $customer)->whereNull('deleted_at')->sum('total');
			$payment = DB::table('customer_payment')->where('date', '<=', $date)->where('customer', '=', $customer)->whereNull('deleted_at')->sum('total');

			$dept -= $payment;
        }
		
		return $dept;
	}

	public function getDeptByOrder($order){
		$order_ids_1 = DB::table('order')->where('date', '<', $order->date)->where('customer', '=', $order->customer)->whereNull('deleted_at')->lists('id');

		$order_ids_2 = DB::table('order')->where('id', '<', $order->id)->where('date', '=', $order->date)->where('customer', '=', $order->customer)->whereNull('deleted_at')->lists('id');

		$order_ids = array_merge($order_ids_1, $order_ids_2);

		if(count($order_ids) == 0){
			$dept = 0;
		}else{
			$dept = DB::table('order')->whereIn('id', $order_ids)->whereNull('deleted_at')->sum('total');
			$payment = DB::table('customer_payment')->whereIn('order', $order_ids)->whereNull('deleted_at')->sum('total');

			$dept -= $payment;
		}

		return $dept;
	}

	public function getOrderPayment($order_id){
		$result = CustomerPayment::where('type', '=', 'payment')
					->where('order', '=', $order_id)
					->sum('total');

		return $result;
	}

	public function getOrderRefund($order_id){
		$result = CustomerPayment::where('type', '=', 'refund')
					->where('order', '=', $order_id)
					->sum('total');

		return $result;
	}

	public function getOrderList($type){
		$result = Order::leftJoin('customer as c', 'c.id', '=', 'order.customer')
						->select('c.id as customer', 'c.name', 'order.id', 'order.date', 'order.subtotal', 'order.total', 'order.discount', 'order.order_number')
						->where('order.deleted_at', '=', NULL)
						->where('c.type', '=', $type)
						->orderBy('order.order_number', 'DESC')
						->get()
						->toArray();

		$customer_payment_model = new CustomerPayment;

		foreach($result as $key => $row){
			$dept = $customer_payment_model->getDept($row['customer'], $row['date'], $row['id']);
			$dept -= $customer_payment_model->getPayment($row['customer'], $row['date'], $row['id']);
			
			$totalpay = $customer_payment_model->getOrderPayment($row['id']);
			$refund   = $customer_payment_model->getOrderRefund($row['id']);

			$result[$key]['dept']     = $dept;
			$result[$key]['totalpay'] = $totalpay;
			$result[$key]['refund']   = $refund;
			$result[$key]['remaining']   = $dept - $totalpay - $refund + $row['total'];
		}

		return $result;
	}

	public function getOrderDetail($order){
		$model = new Order;

		$result = Order::find($order);
		
		return current($result);
	}
}